package cn.smbms.controller;


import cn.smbms.pojo.User;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

//控制器：IndexController
@Controller
//@RequestMapping("/user")
public class IndexController{

    private Logger logger = Logger.getLogger(IndexController.class);
//    @RequestMapping表示方法与哪个请求URL来对应（此处/index）
    @RequestMapping("/index")
    public String index(){
         logger.debug("hello,SpringMVC!");
         return "index";
    }
    @RequestMapping("/welcome")
    public String welcome(@RequestParam(value = "username",required = false)String username){
        logger.debug("welcome,"+username);
        return "index";
    }

    /**
     * 参数传递：controller to view -(ModelAndView)
     * @param username
     * @return
     */
    @RequestMapping("/index1")
    public ModelAndView index(String username){
        logger.debug("welcome! username："+username);
        ModelAndView mView = new ModelAndView();
        mView.addObject("username",username);
        mView.setViewName("index");
        return mView;
    }

    /**
     * 参数传递：controller to view - （Model）
     * @param username
     * @param model
     * @return
     */
    @RequestMapping("/index2")
    public String index(String username, Model model){
        logger.debug("hello,SpringMVC!  username："+username);
        model.addAttribute("username",username);
        model.addAttribute(username);
        User user = new User();
        user.setUserName(username);
        model.addAttribute("currentUser",user);
        model.addAttribute(user);
        return "index";
    }
    /**
     * 参数传递：controller to view - (Map<String,Object>)
     * @param username
     * @param model
     * @return
     */
    @RequestMapping("index3")
    public String index(String username, Map<String,Object> model){
        logger.debug("hello,SpringMVC!  username ："+username);
        model.put("username",username);
        return "index";
    }
}